//
//  GuestCouponHomeViewController.m
//  penzer
//
//  Created by 还带大道 on 2018/11/6.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "GuestCouponHomeViewController.h"
#import "GuestCouponTableViewCell.h"
#import "SPPromptView.h"
#import "SPSelectScrollView.h"
#import "GuestCouponSearchViewController.h"
#import "GuestCouponHeaderFooterView.h"
#import "GuestCardTableViewCell.h"
#import "SPSection.h"
#import "SPPlaceholderView.h"
#import "GuestCouponDetailViewController.h"
#import "GuestCouponWriteOffViewController.h"
#import "MapViewController.h"


@interface GuestCouponHomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *topV;
@property (weak, nonatomic) IBOutlet UIView *searchV;
@property (weak, nonatomic) IBOutlet SPSelectScrollView *topScrollView;
@property (weak, nonatomic) IBOutlet SPPromptView *promptV;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *top_margin;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topVH;
@property (strong, nonatomic) NSArray<SPSection *> *sections;
@property (nonatomic,strong) UILabel *addressL;
@property (nonatomic,assign) NSInteger cellShowCardCount;
@property (nonatomic,assign) CouponViewType viewType;
@property (nonatomic,strong) SPPlaceholderView *placeholderV;

@end

@implementation GuestCouponHomeViewController

- (instancetype)initWithViewType:(CouponViewType)type
{
    if(self = [super init]){
        _viewType = type;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.top_margin.constant = kTopHeight;
    NSString *title = nil;
    switch (_viewType) {
        case CouponViewTypeHome:
            title = @"卡券包";
            break;
        case CouponViewTypeCurrentAvailable:
            title = @"当前可用";
            break;
        case CouponViewTypeSearchResult:
            title = @"搜索结果";
            break;
    }
    [self setNavigation:YES andTitle:title andNaviColor:[UIColor whiteColor] andTitleColor:nil];
    [self setupTableView];
    [self initDataSource];
    self.placeholderV.hidden = YES;
    
}


- (void)initDataSource
{
    SPSection *sec1 = [SPSection new];
    sec1.title = @"卡包";
    sec1.open = YES;
    self.cellShowCardCount = 1;
    
    SPSection *sec2 = [SPSection new];
    sec2.title = @"券";
    sec2.open = YES;
    _sections = @[sec1,sec2];
    
    switch (_viewType) {
        case CouponViewTypeHome:
            self.topVH.constant = 135;
            self.searchV.layer.cornerRadius = 2;
            [self setupTopView];
            [self setupPromptV];
            [self setupRightBtn];
            break;
        case CouponViewTypeCurrentAvailable:
            self.topVH.constant = 0;
            [self setupRightBtn];
            break;
        case CouponViewTypeSearchResult:
            self.topVH.constant = 0;
            [self.view addSubview:self.placeholderV];
            [self.placeholderV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(kTopHeight);
                make.left.right.bottom.equalTo(self.view);
            }];
            break;
    }
    
}



- (void)setupTableView
{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GuestCouponTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GuestCouponTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GuestCardTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GuestCardTableViewCell class])];
    [self.tableView registerClass:[GuestCouponHeaderFooterView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([GuestCouponHeaderFooterView class])];
}



- (void)setupTopView
{
    self.topScrollView.backgroundColor = [UIColor whiteColor];
    NSArray *item = @[@"全部",@"饮食",@"旅游",@"充值",@"旅游",@"充值"];
    self.topScrollView.btnOnClickBlock = ^(NSInteger index) {
        [self topBtnOnClickWithIndex:index];
    };
    self.topScrollView.indicatorBarColor = RZ_COLOR(254, 220, 69);
    self.topScrollView.indicatorBar_height = 3;
    self.topScrollView.item_textColor_nomal = [UIColor blackColor];
    self.topScrollView.item_textColor_selected = [UIColor blackColor];
    self.topScrollView.item_font_nomal = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    self.topScrollView.item_font_selected = [UIFont fontWithName:@"PingFangSC-Medium" size:16];
    self.topScrollView.firstItem_leftMargin = 15;
    self.topScrollView.item_margin = 5;
    self.topScrollView.item_inset = 15;
    self.topScrollView.items = item;
    [self.topScrollView selectedWithIndex:0];
    
}


- (void)topBtnOnClickWithIndex:(NSInteger)index
{
    RZLog(@"----%ld",index);
}



- (void)setupPromptV
{
    self.promptV.closeBlock = ^{
        self.topVH.constant = 100;
        self.promptV.hidden = YES;
    };
}



- (void)setupRightBtn
{
    UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_guest_position"]];
    imageV.contentMode = UIViewContentModeScaleAspectFit;
    [self.naviView addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.navigationController.navigationBar.height);
        make.bottom.equalTo(self.naviView);
        make.right.mas_equalTo(-20);
    }];
    
    UILabel *label = [UILabel new];
    _addressL = label;
    label.text = @"环球中心";
    label.font = [UIFont fontWithName:@"PingFangSC-Light" size:14];
    label.textColor = RZ_COLOR(154, 154, 154);
    [self.naviView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(imageV.mas_left).offset(-5);
        make.centerY.equalTo(imageV);
    }];
    
    UIButton *btn = [UIButton new];
    [btn addTarget:self action:@selector(onClickPostion) forControlEvents:UIControlEventTouchUpInside];
    [self.naviView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label.mas_left);
        make.right.equalTo(imageV.mas_right);
        make.top.bottom.equalTo(label);
    }];
}



- (IBAction)search {
    [self pushSecondVC:[GuestCouponSearchViewController new]];
}


- (void)onClickPostion
{
    [self pushView:[MapViewController new]];
}



#pragma mark--
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = 0;
    if(section == 0){
        count = _sections[section].isOpen?3:1;
    }else{
        count = 3;
    }
    return count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0;
    if(indexPath.section == 0){
        switch (self.cellShowCardCount) {
            case 1:
                height = 130;
                break;
            case 2:
                height = 130;
                break;
            case 3:
                height = 140;
                break;
        }

    }else{
        height = 122;
    }
    return height;
}



- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    GuestCouponHeaderFooterView *headerV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([GuestCouponHeaderFooterView class])];
    if(section == 0){
        headerV.title.text = [NSString stringWithFormat:@"%@ (%d)",_sections[section].title,3];
        headerV.imageV.image = [UIImage imageNamed:@"icon_guest_ka"];
        headerV.rightBtn.hidden = NO;
        headerV.rightBtn.selected = self.sections[section].open;
    }else{
        headerV.title.text = [NSString stringWithFormat:@"%@ (%d)",_sections[section].title,3];
        headerV.imageV.image = [UIImage imageNamed:@"icon_guest_quan"];
        headerV.rightBtn.hidden = YES;
    }
    
    headerV.rightBtnOnClickBlock = ^(UIButton *btn) {
        btn.selected = !btn.isSelected;
        self.sections[section].open = btn.isSelected;
        if(btn.isSelected){
            self.cellShowCardCount = 1;
        }else{
            self.cellShowCardCount = 3;
        }
//        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView reloadData];
    };
    return headerV;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if(indexPath.section == 0){
        GuestCardTableViewCell *cardCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GuestCardTableViewCell class])];
        cardCell.showCardCount = self.cellShowCardCount;
        cardCell.onClickQRcodeBlock = ^{
            GuestCouponWriteOffViewController *vc = [GuestCouponWriteOffViewController new];
            [self presentViewController:vc animated:YES completion:nil];
            
        };
        cell = cardCell;
        
    }else{
        GuestCouponTableViewCell *couponcCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GuestCouponTableViewCell class])];
        cell = couponcCell;
    }
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GuestCouponDetailViewController *detail = [GuestCouponDetailViewController new];
    [self pushView:detail];
}



#pragma mark--去看直播
- (void)gotoLive
{
    
}


#pragma mark--获取卡券包分类数据
- (void)fetchCoupon
{
    
}


#pragma mark--获取搜索数据
- (void)fetchSearchData
{
    
}


#pragma mark--setter&&getter
- (SPPlaceholderView *)placeholderV
{
    if(!_placeholderV){
        _placeholderV = [SPPlaceholderView new];
        _placeholderV.backgroundColor = [UIColor whiteColor];
        [_placeholderV.jumpBtn addTarget:self action:@selector(gotoLive) forControlEvents:UIControlEventTouchUpInside];
        _placeholderV.imageV.image = [UIImage imageNamed:@"icon_search_nodata"];
        _placeholderV.title.text = @"暂无可用卡卷";
        [_placeholderV.jumpBtn setTitle:@"看直播抢卡卷" forState:UIControlStateNormal];
        
        __weak typeof(_placeholderV) weakObj = _placeholderV;
        [_placeholderV.jumpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(weakObj);
            make.size.mas_equalTo(CGSizeMake(134, 35));
        }];
        
        [_placeholderV.title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakObj);
            make.bottom.equalTo(weakObj.jumpBtn.mas_top).offset(-10);
        }];
        
        [_placeholderV.imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakObj);
            make.bottom.equalTo(weakObj.title.mas_top).offset(-10);
        }];
    }
    return _placeholderV;
}

@end
